package hello.app.model;

import hello.app.model.data.HelloContact;
import hello.app.util.Debug;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class HelloContactModel implements Model<HelloContact> {

	private DataSource mDataSource = null;
	private HelloContact[] mData;

	HelloContactModel(Context context) {
		mDataSource = DataSource.getInstance(context);
    }

	public HelloContact[] find(String[] fields, String conditions, String order) {
		SQLiteDatabase db = mDataSource.getWritableDatabase();
		HelloContact[] contacts = null;
        try {
        	Cursor cursor = db.query(
            		HelloContact.TABLE_NAME,
            		fields,
            		conditions,
            		null,
            		null,
            		null,
            		order);
        	int count = cursor.getCount();
        	contacts = new HelloContact[count];
            cursor.moveToFirst();
            for(int i = 0; i < count; i++){
            	contacts[i] = getHelloContact(cursor);
                cursor.moveToNext();
            }
            this.mData = contacts;
        } catch(Exception e){

        } finally {
            db.close();
        }
		return contacts;
	}

	public HelloContact[] findAll() {
		SQLiteDatabase db = mDataSource.getReadableDatabase();
		HelloContact[] contacts = null;
		try {
	            Cursor cursor = db.query(HelloContact.TABLE_NAME, null, null, null, null, null, HelloContact.ID);
	            int count = cursor.getCount();
	            contacts = new HelloContact[count];
	            cursor.moveToFirst();
	            for(int i = 0; i < count; i++){
	            	contacts[i] = getHelloContact(cursor);
	                cursor.moveToNext();
	            }
	            this.mData = contacts;
	        } catch(Exception e){
	        	contacts = new HelloContact[0];
	        } finally {
	            db.close();
	        }
		return contacts;
	}

	public boolean save(HelloContact data) {
		boolean newData = false;
		if(findById(data.getId()) == null){
			newData = true;
		}
		SQLiteDatabase db = mDataSource.getWritableDatabase();
		boolean saved = false;
        try {
        	ContentValues values = new ContentValues();
        	values.put(HelloContact.ID, data.getId());
        	if(data.getName() != null){
        		values.put(HelloContact.NAME, data.getName());
        	}
        	if(data.getAddress() != null){
        		values.put(HelloContact.ADDRESS, data.getAddress());
        	}
        	if(data.getType() != null){
        		values.put(HelloContact.TYPE, data.getType());
        	}
        	values.put(HelloContact.PHONE, data.getPhone());
        	values.put(HelloContact.GOOD, data.getGood());
        	values.put(HelloContact.BAD, data.getBad());
        	values.put(HelloContact.EVALUATED, data.getEvaluated());
            // IDがnullの場合はinsert
            if(newData){
            	db.insert(HelloContact.TABLE_NAME, null, values);
            }else{
                db.update( HelloContact.TABLE_NAME, values, HelloContact.ID + "=?", new String[]{ String.valueOf(data.getId())});
            }
            this.mData = new HelloContact[1];
            mData[0] = data;
            saved = true;
        }catch(Exception e){
        	Debug.error(e.getMessage());
        }finally {
            db.close();
        }
		return saved;
	}

	public HelloContact[] getData(){
		SQLiteDatabase db = mDataSource.getWritableDatabase();
		return mData;
	}

	public HelloContact findByNumber(String number) {
		SQLiteDatabase db = mDataSource.getReadableDatabase();

		HelloContact contact = null;
        try {
            Cursor cursor = db.query( HelloContact.TABLE_NAME, null, HelloContact.PHONE + "=?", new String[]{ String.valueOf(number)}, null, null, null);
            cursor.moveToFirst();
            contact = getHelloContact(cursor);
        } catch(Exception e){

        } finally {
            db.close();
        }
        return contact;
	}

	public HelloContact findById(long id) {
		SQLiteDatabase db = mDataSource.getReadableDatabase();

		HelloContact contact = null;
        try {
            Cursor cursor = db.query( HelloContact.TABLE_NAME, null, HelloContact.ID + "=?", new String[]{ String.valueOf(id)}, null, null, null);
            cursor.moveToFirst();
            contact = getHelloContact(cursor);
        } catch(Exception e){

        } finally {
            db.close();
        }
        return contact;
	}

	 private HelloContact getHelloContact( Cursor cursor){
		 HelloContact contact = new HelloContact(cursor.getLong(0));
		 contact.setName(cursor.getString(1));
		 contact.setPhone(cursor.getString(2));
		 contact.setGood(cursor.getInt(3));
		 contact.setBad(cursor.getInt(4));
		 contact.setEvaluated(cursor.getInt(5));
		 contact.setAddress(cursor.getString(6));
		 contact.setType(cursor.getString(7));
		 return contact;
	}

	public boolean delete(long id) {
		return false;
	}

}
